//Features a agregar:
//deberia tener un metodo render
//deberia haber un metodo HasResetButton [true|false] y deberia poder crearse/agregarse dicho boton

var ChartControl = (function(){
	var offset = 0.025;
	var seriesCollection = [];
	var hash_index_table = [];
	var plot = null;
	var settings = null;
	
	return {
		init : function(config){			
			settings = config;
			offset = settings.getOffset();
			$.jqplot.config.enablePlugins = true;
			$(settings.getResetID()).click(function(){
				plot.resetZoom();
			});
			console.log(settings);
		},
		destroy : function(){
			plot = null;
		},
		createPlot : function(){
			var dataset = []; 
			for (var i = 0; i < seriesCollection.length; i++){
				dataset[i] = seriesCollection[i].getData();
			}
									
			try{
				plot = $.jqplot(settings.getContainerID(), dataset,{
					title: settings.getChartTitle(),
					series: this.getSeriesConf(),
					cursor: {
						tooltipLocation:'sw',
						show:true,
						zoom:true,
						clickReset:true,
						showVerticalLine:true,
						showHorizontalLine:true,
						showCursorLegend:true
					},
					highlighter: {show: false},
					axes:{
							xaxis:{
								label: settings.getXAxisLabel(),
								autoscale:true,
								labelRenderer:$.jqplot.CanvasAxisLabelRenderer, 
								min : settings.getXMin(),
								max : settings.getXMax()
							},
							yaxis:{
								label: settings.getYAxisLabel(),
								autoscale:true,
								tickOptions:{formatString:'%.3f'},
								labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
								min: settings.getYMin(),
								max: settings.getYMax()
							}
					}
				});
			}catch(err){
				console.log(err);
			}
		},
		addSeries : function(series){
			var pos = seriesCollection.length;
			seriesCollection[pos] = series;
			hash_index_table[series.getId()] = pos;
			return;
		},
		getSeriesConf: function(){
			var seriesConf = [];
			for (var i = 0; i < seriesCollection.length; i++){
				seriesConf[i] = seriesCollection[i].getConf();
			}
			return seriesConf;
		},
		getSeriesIndex: function(identifier){
			return hash_index_table[identifier];
		},
		feedData : function(url){
			var that = this;
			$.get(url,function(reply){
				var obj = eval('(' + reply + ')');
				that.repaint(obj.data,parseFloat(obj.minimo),parseFloat(obj.maximo),parseFloat(obj.nsamples));
			});
		},
		paint : function(){
			this.createPlot();
		},
		repaint : function(samples, minY, maxY, numSamples){
			plot.series[0].data = samples;
	    	plot.resetAxesScale();
	    	plot.axes.xaxis.min = 0;
			plot.axes.xaxis.max = numSamples;
			plot.axes.yaxis.min = minY - offset;
			plot.axes.yaxis.max = maxY + offset;
			plot.replot();
		},
		toggleSeries : function(identifier){
			this.toggle(this.getSeriesIndex(identifier));
		},
		toggle : function(seriesIndex){
			var toggle = plot.series[seriesIndex].show; 
			plot.series[seriesIndex].show = !toggle; 
			plot.replot(); 
		}
	};
}());